<?php

class Core_Frontend_Component_Map_Controller_Show extends Dcmp_Component_Controller {

    public function poiAction($id = null, $width = 200, $height = 200) {
        $ids = explode(',', $id);
        $query = Kd_Data_Factory::getQueryDcl()
                        ->from('{t}customer_map', 'a')
                        ->rightJoin('foundation_poi', 'b', 'b.id', 'a.poi_id')
                        ->contain('b.id', ':poiIds:s')
                        ->addParameter('poiIds', $ids)
                        ->fetch();
        return $this->templateResult(array('pois' => $query, 'width' => $width, 'height' => $height));
    }

    public function roadAction($id, $poi = NULL, $width = 500, $height = 500) {
        $map = Kd_Data_Factory::getQueryDcl()
                        ->from('{t}map')
                        ->eq('map_name', ':name:s')
                        ->eq('status', ':status:s')
                        ->addParameter('name', $id)
                        ->addParameter('status', 'valid')
                        ->fetch()
                        ->getFirstRow();
        $element = Kd_Data_Factory::getQueryDcl()
                        ->from('{t}map_element', 'a')
                        ->innerJoin('{t}map_element_associated', 'b', 'a.element_id', 'b.old_element_id')
                        ->eq('b.old_map_id', ':mapId:i')
                        ->eq('a.type', '2')
                        ->addParameter('mapId', $map['map_id'])
                        ->limit(1)
                        ->fetch()
                        ->getFirstRow();
        $element['coordinates'] = Kd_Data_Factory::getQueryDcl()
                        ->from('{t}t_map_latlng')
                        ->eq('element_id', ':elementId:i')
                        ->addParameter('elementId', $element['old_element_id'])
                        ->fetch();

        $pois = array();
        if (!empty($poi)) {
            $poiIds = explode(',', $poi);
            $pois = Kd_Data_Factory::getQueryDcl()
                            ->from('{t}customer_map', 'a')
                            ->rightJoin('foundation_poi', 'b', 'b.id', 'a.poi_id')
                            ->contain('b.id', ':poiIds:s')
                            ->addParameter('poiIds', $poiIds)
                            ->fetch();
        }
        return $this->templateResult(array('map' => $map, 'element' => $element, 'pois' => $pois, 'width' => $width, 'height' => $height));
    }

}

